package cn.lvji.springboot.learn5.service.impl;

import cn.lvji.springboot.learn5.dao.ScenicRepository;
import cn.lvji.springboot.learn5.entity.Scenic;
import cn.lvji.springboot.learn5.service.ScenicService;
import cn.lvji.springboot.learn5.vo.LayuiGridResponse;
import cn.lvji.springboot.learn5.vo.Response;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Order;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author zhouzx
 */
@Service
public class ScenicServiceImpl implements ScenicService {

    private final ScenicRepository scenicRepository;

    @Autowired
    public ScenicServiceImpl(ScenicRepository scenicRepository) {
        this.scenicRepository = scenicRepository;
    }

    @Override
    public Response<Void> save(Scenic scenic) {
        scenic.setCreateTime(new Date());
        scenic.setUpdateTime(new Date());
        scenicRepository.save(scenic);
        return Response.SUCCESS;

    }

    @Override
    public Scenic get(Long id) {
        return scenicRepository.findById(id).get();
    }


    @Override
    public Response<Void> update(Scenic scenic) {
        Scenic db = scenicRepository.findById(scenic.getId()).get();
        BeanUtils.copyProperties(scenic,db,"createTime");
        db.setUpdateTime(new Date());
        scenicRepository.save(db);
        return Response.SUCCESS;
    }

    @Override
    public Response<Void> delete(Long id) {
        scenicRepository.deleteById(id);
        return Response.SUCCESS;
    }

    @Override
    public LayuiGridResponse<Scenic> list(int page, int limit, String name, Date createTime) {
        Pageable pageable = PageRequest.of((page - 1) , limit,
                Sort.by(Order.desc("createTime"), Order.desc("id")));

        Page<Scenic> list = scenicRepository.findAll((Specification<Scenic>) (root, query, criteriaBuilder) -> {
            List<Predicate> list1 = new ArrayList<>();
            if (!StringUtils.isEmpty(name)) {
                list1.add(criteriaBuilder.like(root.get("name").as(String.class), "%" + name + "%"));
            }

            if (createTime != null) {
                list1.add(criteriaBuilder.greaterThanOrEqualTo(root.get("createTime"), createTime));
            }
            Predicate[] p = new Predicate[list1.size()];
            return criteriaBuilder.and(list1.toArray(p));
        }, pageable);

        LayuiGridResponse<Scenic> response = new LayuiGridResponse<>();
        response.setCode(0);
        response.setCount(list.getTotalElements());
        response.setData(list.getContent());
        response.setMsg("SUCCESS");
        return response;
    }
}
